//
//  HLMineViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/20.
//

#import "HLMineViewController.h"
#import "HLBalanceModel.h"
#import "HLUserInfoRequest.h"
#import "HLFollowCountRequest.h"
#import "HLWalletRequest.h"
#import "HLBannerRequest.h"

#import "HLMineCell.h"
#import "HLMineHeaderView.h"
#import "HLMineFooterView.h"

#import "HLSettingViewController.h"
#import "HLEditInfoViewController.h"
#import "HLUserDetailViewController.h"
#import "HLVideoPostViewController.h"

#import "HLDiscountRequest.h"
#import "HLHomeDiscountView.h"
#import "HLVIPRequest.h"

@interface HLMineViewController ()

@property (nonatomic, strong) UIImageView *headerBackgoundView;
@property (nonatomic, strong) HLUserInfoModel *userModel;
@property (nonatomic, strong) NSArray <HLBannerModel *> *banners;
@property (nonatomic, strong) NSString *amount;

@property (nonatomic, strong) HLHomeDiscountView *discountView;
@end

@implementation HLMineViewController


- (instancetype)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self){
        self.userModel = [HLRunTime shareInstance].mine;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.request && !self.request.isExecuting){
        [self.request start];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HLBackgroundColor;
    self.headerBackgoundView = [[UIImageView alloc] init];
    _headerBackgoundView.image = UIImageMake(@"mine_header_background");
    [self.view insertSubview:_headerBackgoundView atIndex:0];
    [_headerBackgoundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    
    self.request = [[HLUserInfoRequest alloc] initWithMe];
    self.request.delegate = self;
    [self.request start];
    
    HLBannerRequest *bannerRequest = [[HLBannerRequest alloc] initWithType:HLBannerPositionTypeBanner];
    [bannerRequest startWithCompletionBlockWithSuccess:^(__kindof HLBannerRequest * _Nonnull request) {
        if (request.isSuccess){
            self.banners = request.bannerList;
            [self.tableView reloadData];
        }
    } failure:nil];
    [self setupDiscountView];
}

- (void)setupDiscountView
{
    if ([HLRunTime shareInstance].isFood) {
        return;
    }
    
    HLDiscountRequest *homeListRequest = [[HLDiscountRequest alloc] init];
    [homeListRequest startWithCompletionBlockWithSuccess:^(__kindof HLDiscountRequest * _Nonnull request) {
        if (request.isSuccess){
            self.discountView = [[HLHomeDiscountView alloc] init];
            [self.view addSubview:self.discountView];
            [self.discountView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(0);
                make.size.mas_equalTo(CGSizeMake(140, 68));
                make.bottom.mas_equalTo(-(SafeAreaInsetsConstantForDeviceWithNotch.bottom + 68));
            }];
            self.discountView.cycleDatas = [request discountList];
        }
    } failure:nil];
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithImage:UIImageMake(@"mine_nav_edit") style:UIBarButtonItemStylePlain target:self action:@selector(editAction)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    self.navigationItem.rightBarButtonItems = @[edit, space];
}

- (void)editAction
{
    MJWeakSelf
    HLEditInfoViewController *editInfoVC = [[HLEditInfoViewController alloc] initWithModel:self.userModel callback:^(HLUserInfoModel *user) {
        [weakSelf.request start];
    }];
    [self.navigationController pushViewController:editInfoVC animated:YES];
}

- (void)initTableView
{
    [super initTableView];
    
    [self.datasources addObjectsFromArray:[HLMineCellModel cellWithMine]];
    
    self.tableView.backgroundColor = UIColorClear;
    [self.tableView registerClass:[HLMineCell class] forCellReuseIdentifier:[HLMineCell identifyCell]];
    [self.tableView registerClass:[HLMineFooterView class] forHeaderFooterViewReuseIdentifier:[HLMineFooterView identifyCell]];
    [self.tableView registerClass:[HLMineHeaderView class] forHeaderFooterViewReuseIdentifier:[HLMineHeaderView identifyCell]];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorClear];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasources.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLMineCell identifyCell]];
    cell.model = self.datasources[indexPath.row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HLMineHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[HLMineHeaderView identifyCell]];
    headerView.model = self.userModel;
    headerView.incomeView.amountLabel.text = self.amount;
    [headerView.infoView.previewButton addTarget:self action:@selector(previewAction) forControlEvents:UIControlEventTouchUpInside];
    [headerView.infoView.avatarTap addTarget:self action:@selector(previewAction)];
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    HLMineFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[HLMineFooterView identifyCell]];
    
    footerView.banners = self.banners;
    
    return footerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLMineCellModel *model = self.datasources[indexPath.row];
    
    switch (model.cellSpot) {
        case HLMineCellSpotMissionCenter:
        {
            [HLUIHelper h5WithURL:HLH5ExchangeUrl controller:self];
        }
            break;
        case HLMineCellSpotSetting:
        {
            HLSettingViewController *settingVC = [[HLSettingViewController alloc] init];
            [self.navigationController pushViewController:settingVC animated:YES];
        }
            break;
        case HLMineCellSpotVideo:
        {
            HLVideoPostViewController *videoPostVC = [[HLVideoPostViewController alloc] init];
            [self.navigationController pushViewController:videoPostVC animated:YES];
        }
            break;

        case HLMineCellSpotCustomer:
        {
           // [self contactUS];
        }
            break;
        case HLMineCellSpotInvitation:
        {
            [HLUIHelper h5WithURL:HLH5InvitationUrl controller:self];
        }
            break;
        default:
            break;
    }
}

- (void)previewAction
{
    MJWeakSelf
    HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initMeWithCallback:^(id u) {
        [weakSelf.request start];
    }];
    [self.navigationController pushViewController:userDetailVC animated:YES];
}


- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    HLUserInfoRequest *userInfoRequest = (HLUserInfoRequest *)request;
    if (userInfoRequest.isSuccess){
        self.userModel = [userInfoRequest user];
        [self.tableView reloadData];
        [self walletAmountRequest];
    }
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [super requestFailed:request];
}

- (void)walletAmountRequest
{
    MJWeakSelf
    HLWalletRequest *walletRequest = [[HLWalletRequest alloc] initWithType:HLWalletTypeBalance params:@{}];
    [walletRequest startWithCompletionBlockWithSuccess:^(__kindof HLWalletRequest * _Nonnull request) {
        if (request.isSuccess){
            self.amount = [request balance];
            [self.tableView reloadData];
        }
    } failure:nil];
    [[HLVIPRequest.alloc init] startWithCompletionBlockWithSuccess:^(__kindof HLVIPRequest * _Nonnull request) {
        if (request.isSuccess) {
            weakSelf.userModel.isVip = [request VIPInforResponse].isVip;
            [self.tableView reloadData];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}

//- (void)contactUS {
//
//    CSVisitorChatViewController *chatVC = [[CSVisitorChatViewController alloc] initWithArg:CS53_ARG style:@"1"];
//    chatVC.title = @"客服";
//    chatVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:chatVC animated:YES];
//    chatVC.hidesBottomBarWhenPushed = NO;
//}
//
//
//#pragma mark - CS53ServiceDelegate
//
//- (void)didFinishLoad{
//    [[CS53Manager sharedManager] loadChatList];
//    CSCustomInfoModel *model = [[CSCustomInfoModel alloc] init];
//    model.visitorId = [HLRunTime shareInstance].mine.userYueId;
//    model.arg = CS53_ARG;
//    model.userId = @"";
//    model.username = [HLRunTime shareInstance].mine.nickName;
//    model.phone = [HLRunTime shareInstance].mine.mobileNo;
//    [[CS53Manager sharedManager] registerCustomInfo:model];
//}

@end
